/*
 * 描述 @version 1 
 * Create by cy on 2018/10/13
 * */
const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');//引入mongoose是为了User
const User = mongoose.model('users');//是为了那users进行验证
const keys = require('../config/key');

const opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretOrKey;

module.exports=passport=>{
    passport.use(new JwtStrategy(opts, (jwt_payload, done)=> {
        // console.log(jwt_payload);done是一个回调函数
        User.findById(jwt_payload.id).then((user)=>{
            if(user){//根据ID查询,如果成功返回用户信息
                return done(null,user);
            }else{
                return done(null,false);
            }
        })
    }));
};
